//打家劫舍 II
//测试链接：https://leetcode.cn/problems/house-robber-ii/description/
public class Rob {
    public int rob(int[] nums) {
        int n = nums.length;
        return Math.max(nums[0] + rob1(nums, 2, n - 2), rob1(nums, 1, n - 1));
    }

    public int rob1(int[] nums, int left, int right) {
        if (left > right)
            return 0;
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[left] = nums[left];
        for (int i = left + 1; i <= right; i++)

        {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(g[i - 1], f[i - 1]);
        }
        return Math.max(f[right], g[right]);
    }
}
